/*
 * @Author: 千锋爱佛僧
 * @公众号: 大前端私房菜
 * @Slogan: 千锋精品教程，好学得不像实力派！
 */
import { createRouter, createWebHashHistory } from "vue-router";
import store from "@/store"; //store等同于组件中this.$store
export const routes = [
  {
    path: "/",
    name: "main",
    component: () => import("../layout/index.vue"),
    children: [
      {
        path: "/dashboard",
        component: () => import("../views/dashboard/dashboard.vue"),
        meta: {
          label: "数据可视化",
          icon: "PieChart",
        },
      },
      {
        path: "/classify",
        component: () => import("../views/classify/index"),
        meta: {
          label: "分类管理",
          icon: "Operation",
        },
        children: [
          {
            path: "/classify/list",
            component: () => import("../views/classify/list.vue"),
            meta: {
              label: "分类列表", //自定义的附带信息
            },
          },
          {
            path: "/classify/pub",
            component: () => import("../views/classify/pub.vue"),
            meta: {
              label: "发布分类",
            },
          },
          {
            path: "/classify/edit",
            component: () => import("../views/classify/edit.vue"),
            meta: {
              label: "编辑分类",
              hidden: true,
            },
          },
        ],
      },
      {
        path: "/company",
        component: () => import("../views/company/index"),
        meta: {
          label: "企业管理",
          icon: "OfficeBuilding",
        },
        children: [
          {
            path: "/company/pub",
            component: () => import("../views/company/pub.vue"),
            meta: {
              label: "企业入驻",
            },
          },
        ],
      },
      {
        path: "/job",
        component: () => import("../views/job/index"),
        meta: {
          label: "岗位管理",
          icon: "Coordinate",
        },
        children: [
          {
            path: "/job/pub",
            component: () => import("../views/job/pub.vue"),
            meta: {
              label: "发布岗位",
            },
          },
          {
            path: "/job/list",
            component: () => import("../views/job/list.vue"),
            meta: {
              label: "岗位列表",
            },
          },
        ],
      },
      {
        path: "/manager",
        component: () => import("../views/manager/index"),
        meta: {
          label: "权限管理",
          icon: "SetUp",
        },
        children: [
          {
            path: "/manager/role",
            component: () => import("../views/manager/role.vue"),
            meta: {
              label: "角色管理",
            },
          },
          {
            path: "/manager/user",
            component: () => import("../views/manager/user.vue"),
            meta: {
              label: "账号管理",
            },
          },
        ],
      },
      {
        path: "/excel",
        component: () => import("../views/excel/index"),
        meta: {
          label: "Excel表格",
          icon: "Notebook",
        },
        children: [
          {
            path: "/excel/export",
            component: () => import("../views/excel/export-excel.vue"),
            meta: {
              label: "表格导出",
            },
          },
          {
            path: "/excel/import",
            component: () => import("../views/excel/import-excel.vue"),
            meta: {
              label: "Excel导入",
            },
          },
        ],
      },
      {
        path: "/permission",
        component: () => import("@/views/permission.vue"),
        meta: {
          label: "无权限",
          hidden: true,
        },
      },
    ],
  },
  {
    path: "/login",
    component: () => import("@/views/login.vue"),
  },
  {
    path: "/vuex/test",
    component: () => import("@/views/vuex-test/index.vue"),
  },
];

const router = createRouter({
  history: createWebHashHistory(),
  routes,
});

// router全局前置守卫
// let isLogin = true; //模拟登录状态
router.beforeEach((to, from, next) => {
  // console.log(to, from, next);
  store.commit("bread/initRoutePathMute", to.path);
  let userInfo = store.state.user.userInfo;
  if (!["/login", "/permission"].includes(to.path)) {
    if (userInfo) {
      let { checkedKeys } = userInfo; //获取当前用户有权访问的路由
      let whiteList = ["/", "/excel/import", "/excel/export"].concat(
        checkedKeys
      );
      let bool = whiteList.includes(to.path); //用户想要访问的路由，是否存在于权限数组中
      if (bool) {
        next(); //有权限用户直接放行
      } else {
        next("/permission");
      }
    } else {
      next("/login"); //没登录的用户，强行跳转到登录
    }
  } else {
    next(); //login、permission直接放行
  }
});

export default router;
